package com.android.panballgame;

import java.util.Timer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.annotation.SuppressLint;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;

public class PBGameRenderer3 implements Renderer{

	static float usetime;
	//실제 도형
	Context context;	
	private PBBackground background;
	private PBVector [] vec; //코인 위치 좌표
	private PBCoin [] coin;
	private PBClock clock;	//시간 줄여주는 아이템
	private PBClock speed; //공 속도 빠르게 하는 아이템	
	private PBBall ball;
	private PBVector clockVec;
	private PBVector speedVec;
	
	//경계 도형
	private PBCircle CirBall; //공
	private PBCircle[] CirCoin; //코인
	private PBCircle CirHole; //골인 구멍	
	private PBRectangle [] rec; //벽(장애물)
	
	private PBCircle [] CirMove; //순간 이동 구멍
	private PBCircle CirClock; //아이템
	private PBCircle CirSpeed; 
	
	//가속도 계산 변수
	int oriX, oriY= 0;
	float sx, sy, vx, vy = 0;	
	
	float cs = 0.015f; //가속도 조절 변수 초기화
	
	//조건 변수
	boolean drawball; //공 골인 유무
	boolean [] inhole; //순간이동 구멍에 들어갔는가
		
	// 생성자
	@SuppressLint("NewApi")
	public PBGameRenderer3(Context context){
		
		if(PBEngine.useDb == 0){
			PBEngine.ballx = 0.5f;
			PBEngine.bally = 9.6f;
		}
		
		this.context = context;
		//각 도형 생성
		background = new PBBackground();
		coin = new PBCoin[9];
		vec = new PBVector[9];	
		ball = new PBBall();
		
		for(int i = 0; i < 9; i++)
			coin[i] = new PBCoin();
		
		int ran1;
		if(PBEngine.useDb == 1)
			ran1 = PBEngine.coinLoc;
		else{
			ran1 = 1 + (int)(Math.random() * 5);
			PBEngine.coinLoc = ran1;
		}
		switch(ran1){
			case 1 : 
				vec[0] = new PBVector(1.6f, 1.5f);
				vec[1] = new PBVector(4.4f, 6.3f);
				vec[2] = new PBVector(4.4f, 0.2f);
				vec[3] = new PBVector(8.5f, 2.7f);
				vec[4] = new PBVector(9.9f, 6.3f);
				vec[5] = new PBVector(11.2f, 0.2f);
				vec[6] = new PBVector(16.9f, 7.6f);
				vec[7] = new PBVector(16.9f, 6.3f);
				vec[8] = new PBVector(17.1f, 2.8f);
				break;
			case 2 :				 
				vec[0] = new PBVector(0.5f, 6.4f);
				vec[1] = new PBVector(4.4f, 2.6f);
				vec[2] = new PBVector(8.3f, 10.0f);
				vec[3] = new PBVector(9.9f, 2.7f);
				vec[4] = new PBVector(11.9f, 6.4f);
				vec[5] = new PBVector(14.2f, 0.2f);
				vec[6] = new PBVector(6.9f, 0.2f);
				vec[7] = new PBVector(15.9f, 5.1f);
				vec[8] = new PBVector(17.1f, 2.8f);
				break;
			case 3 :		
				vec[0] = new PBVector(3.0f, 1.5f);
				vec[1] = new PBVector(3.0f, 10.0f);
				vec[2] = new PBVector(5.8f, 9.5f);
				vec[3] = new PBVector(7.2f, 5.6f);
				vec[4] = new PBVector(12.0f, 1.45f);
				vec[5] = new PBVector(11.2f, 0.2f);
				vec[6] = new PBVector(14.2f, 5.1f);
				vec[7] = new PBVector(14.2f, 7.9f);
				vec[8] = new PBVector(18.5f, 6.2f);
				break;
			case 4 :		
				vec[0] = new PBVector(5.1f, 0.2f);
				vec[1] = new PBVector(8.8f, 0.2f);
				vec[2] = new PBVector(1.5f, 0.2f);
				vec[3] = new PBVector(4.3f, 6.22f);
				vec[4] = new PBVector(7.1f, 6.3f);
				vec[5] = new PBVector(12.6f, 5.1f);
				vec[6] = new PBVector(15.5f, 2.7f);
				vec[7] = new PBVector(17.0f, 6.2f);
				vec[8] = new PBVector(18.35f, 8.8f);
				break;
			case 5 :	
				vec[0] = new PBVector(0.5f, 6.4f);
				vec[1] = new PBVector(4.4f, 2.6f);
				vec[2] = new PBVector(8.3f, 10.0f);
				vec[3] = new PBVector(7.2f, 5.6f);
				vec[4] = new PBVector(9.9f, 6.3f);
				vec[5] = new PBVector(11.2f, 0.2f);
				vec[6] = new PBVector(14.2f, 5.1f);
				vec[7] = new PBVector(14.2f, 7.9f);
				vec[8] = new PBVector(18.5f, 6.2f);
				break;
		}
		
		clock = new PBClock();
		speed = new PBClock();
		
		int ran2;
		if(PBEngine.useDb == 1)
			ran2 = PBEngine.itemLoc;
		else{
			ran2 = 1 + (int)(Math.random() * 3);
			PBEngine.itemLoc = ran2;
		}
		switch(ran2){
			case 1 :
				clockVec = new PBVector(8.5f, 1.4f);
				speedVec = new PBVector(11.45f, 7.6f);
				break;
			case 2 :
				clockVec = new PBVector(12.8f, 8.77f);
				speedVec = new PBVector(15.5f, 7.6f);
				break;
			case 3 :
				clockVec = new PBVector(4.4f, 7.7f);
				speedVec = new PBVector(15.5f, 0.2f);
				break;
		}
		
		//경계 도형 생성
		CirBall = new PBCircle((PBEngine.ballx + 0.475f), (PBEngine.bally + 0.475f), 0.475f); //공
		CirCoin = new PBCircle[9]; //코인
		for(int i = 0; i < 9; i++){
			CirCoin[i] = new PBCircle((vec[i].x + 0.475f), (vec[i].y + 0.475f), 0.475f);
		}		
		CirHole = new PBCircle(18.845f, 0.813f, 0.475f); //골인 구멍 
		
		CirMove = new PBCircle[3]; // 순간이동구멍
		CirMove[0] = new PBCircle(3.05f + 0.475f, 8.9f + 0.475f, 0.475f);
		CirMove[1] = new PBCircle(15.47f + 0.475f, 8.67f + 0.475f, 0.475f);
		CirMove[2] = new PBCircle(14.22f + 0.475f, 1.52f + 0.475f, 0.475f);		
		
		CirClock =  new PBCircle((clockVec.x + 0.475f), (clockVec.y + 0.475f), 0.475f); // 시계(아이템)
		CirSpeed =  new PBCircle((speedVec.x + 0.475f), (speedVec.y + 0.475f), 0.475f); // 속도빠르게 아이템
		
		rec = new PBRectangle[44];//사각형들
		//X축과 나란한 벽
		rec[0] = new  PBRectangle(1.33f, 1.25f, 1.5f, 0.11f);
		rec[1] = new  PBRectangle(4.11f, 1.25f, 2.88f, 0.11f);
		rec[2] = new  PBRectangle(9.65f, 1.25f, 2.93f, 0.11f);
		rec[3] = new  PBRectangle(13.89f, 1.25f, 2.95f, 0.11f);		
		rec[4] = new  PBRectangle(0f, 2.49f, 1.44f, 0.11f);
		rec[5] = new  PBRectangle(8.27f, 2.49f, 4.31f, 0.11f);
		rec[6] = new  PBRectangle(16.73f, 2.49f, 2.95f, 0.11f);		
		rec[7] = new  PBRectangle(8.27f, 3.72f, 2.87f, 0.11f);		
		rec[8] = new  PBRectangle(1.33f, 4.335f, 1.5f, 0.11f);		
		rec[9] = new  PBRectangle(18.15f, 4.335f, 1.53f, 0.11f);		
		rec[10] = new  PBRectangle(9.65f, 4.95f, 2.93f, 0.11f);		
		rec[11] = new  PBRectangle(0f, 6.19f, 1.44f, 0.11f);		
		rec[12] = new  PBRectangle(11.03f, 6.19f, 4.39f, 0.11f);		
		rec[13] = new  PBRectangle(1.33f, 7.43f, 1.5f, 0.11f);
		rec[14] = new  PBRectangle(4.11f, 7.43f, 1.5f, 0.11f);		
		rec[15] = new  PBRectangle(9.65f, 7.43f, 2.93f, 0.11f);
		rec[16] = new  PBRectangle(15.31f, 7.43f, 2.95f, 0.11f);		
		rec[17] = new  PBRectangle(0, 8.63f, 1.44f, 0.11f);
		rec[18] = new  PBRectangle(2.72f, 8.63f, 1.39f, 0.11f);		
		rec[19] = new  PBRectangle(8.37f, 8.63f, 4.31f, 0.11f);
		rec[20] = new  PBRectangle(16.73f, 8.63f, 2.95f, 0.11f);		
		rec[21] = new  PBRectangle(6.88f, 9.83f, 7.12f, 0.11f);
		rec[22] = new  PBRectangle(15.31f, 9.83f, 2.95f, 0.11f);
		
		//Y축과 나란한 벽
		rec[23] = new  PBRectangle(1.33f, 1.25f, 0.11f, 1.35f);
		rec[24] = new  PBRectangle(1.33f, 3.72f, 0.11f, 1.34f);
		rec[25] = new  PBRectangle(1.33f, 7.43f, 0.11f, 1.31f);		
		rec[26] = new  PBRectangle(2.72f, 1.25f, 0.11f, 4.43f);
		rec[27] = new  PBRectangle(2.72f, 8.63f, 0.11f, 2.51f);			
		rec[28] = new  PBRectangle(4.11f, 1.25f, 0.11f, 2.58f);
		rec[29] = new  PBRectangle(4.11f, 4.95f, 0.11f, 3.79f);		
		rec[30] = new  PBRectangle(5.5f, 2.49f, 0.11f, 7.45f);		
		rec[31] = new  PBRectangle(6.88f, 1.25f, 0.11f, 8.69f);		
		rec[32] = new  PBRectangle(8.27f, 0f, 0.11f, 3.83f);	
		rec[33] = new  PBRectangle(8.27f, 4.95f, 0.11f, 3.79f);		
		rec[34] = new  PBRectangle(9.65f, 4.95f, 0.11f, 2.59f);		
		rec[35] = new  PBRectangle(12.47f, 0f, 0.11f, 1.36f);		
		rec[36] = new  PBRectangle(12.47f, 2.49f, 0.11f, 2.57f);		
		rec[37] = new  PBRectangle(12.47f, 7.43f, 0.11f, 1.31f);		
		rec[38] = new  PBRectangle(13.89f, 1.25f, 0.11f, 3.81f);
		rec[39] = new  PBRectangle(13.89f, 6.19f, 0.11f, 3.75f);		
		rec[40] = new  PBRectangle(15.31f, 3.72f, 0.11f, 3.82f);		
		rec[41] = new  PBRectangle(16.73f, 0f, 0.11f, 1.36f);		
		rec[42] = new  PBRectangle(16.73f, 2.49f, 0.11f, 2.57f);		
		rec[43] = new  PBRectangle(18.15f, 6.19f, 0.11f, 3.75f);
		
		//조건변수 초기화
		drawball = true;
		
		if(PBEngine.useDb != 1){
			for(int i = 0; i < 9; i++)
				PBEngine.drawcoin[i] = 1;			
		}
		
		inhole = new boolean[3];
		for(int i = 0; i < 3; i++)
			inhole[i] = false;	
		
		if(PBEngine.useDb != 1){
			PBEngine.item1 = 1;
			PBEngine.item2 = 1;
		}
	}
	
		@SuppressLint("NewApi")
		@Override
		public void onDrawFrame(GL10 gl) {

			try{
				Thread.sleep(PBEngine.GAME_THREAD_FPS_SLEEP);
			}catch (InterruptedException e){
				e.printStackTrace();
			}		
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);			
			gl.glEnable(GL10.GL_BLEND);
			gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
			
			for(int i = 0; i < 9; i++){//코인이 먹힌 경우
				if(PBOverlapTester.overlapCircles(CirBall, CirCoin[i]))			
					if(PBEngine.drawcoin[i] == 1){
						PBEngine.drawcoin[i] = 0;
						PBGame.sound(1);
						PBEngine.remain--;
					}
			}
			
			if(PBOverlapTester.overlapCircles(CirBall, CirClock))//시계 아이템		
				if(PBEngine.item1 == 1){
					PBEngine.item1 = 0;
					PBGame.sound(3);
					PBEngine.MINUS_TIME = true;
			}
			
			if(PBOverlapTester.overlapCircles(CirBall, CirSpeed))//스피드 올려주는 아이템		
				if(PBEngine.item2 == 1){
					PBEngine.item2 = 0;
					cs = 0.03f;
					PBGame.sound(5);
				}
						
			for(int i = 0; i < 3; i++){
				if(PBOverlapTester.pointInCircle(CirMove[i], CirBall.center)){//순간 이동 구멍	
					inhole[i] = true;
					PBGame.sound(2);
				}
			}
			
			if(PBOverlapTester.pointInCircle(CirHole, CirBall.center))//공이 골인 지점에 들어간 경우
				if(drawball){
					if(PBEngine.remain <= 0){ //남은 과제 수가 0이면
						PBGame.sound(2);
						drawball = false;
						usetime = PBEngine.time;
						Timer timer = new Timer();
						timer.schedule(new PBDelay(), 1000);
						PBEngine.STATUS = PBEngine.CLEAR;
						PBEngine.WAIT = true;
					}else{
						PBEngine.HOLE = true;
					}
				}
				
			background(gl);
			coin(gl);
			if(PBEngine.item1 == 1)
				clock(gl);
			if(PBEngine.item2 == 1)
				speed(gl);
			if(drawball)	
				ball(gl);
		}
		

		//벽(장애물)에 부딪혔을 때
		private boolean obstacleS(){//남쪽
			boolean crash = false;			
			for(int i = 0; i < 44; i++){				
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 1){
					if( Math.abs(CirBall.center.y - rec[i].lowerLeft.y) >= Math.abs(CirBall.center.x - rec[i].lowerLeft.x))
						PBEngine.bally = rec[i].lowerLeft.y - CirBall.radius - CirBall.radius;
					else
						crash = true;
				}			
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 2){
					PBEngine.ballx = rec[i].lowerLeft.x - CirBall.radius - CirBall.radius;
					crash = true;
				}
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 3){
					if( Math.abs(CirBall.center.y - (rec[i].lowerLeft.y + rec[i].height)) >= Math.abs(CirBall.center.x - rec[i].lowerLeft.x))
						PBEngine.bally =  rec[i].lowerLeft.y + rec[i].height; 
					else
						crash = true;
				}	
			}			
			return crash;
		}
		
		private boolean obstacleN(){//북쪽
			boolean crash = false;			
			for(int i = 0; i < 44; i++){			
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 7){
					if( Math.abs(CirBall.center.y - rec[i].lowerLeft.y) >= Math.abs(CirBall.center.x - (rec[i].lowerLeft.x + rec[i].width)))
						PBEngine.bally = rec[i].lowerLeft.y - CirBall.radius - CirBall.radius;
					else
						crash = true;
				}			
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 8){
					PBEngine.ballx = rec[i].lowerLeft.x + rec[i].width;
					crash = true;
				}
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 9){
					if( Math.abs(CirBall.center.y - (rec[i].lowerLeft.y + rec[i].height)) >= Math.abs(CirBall.center.x - (rec[i].lowerLeft.x + rec[i].width)))
						PBEngine.bally =  rec[i].lowerLeft.y + rec[i].height; 
					else
						crash = true;
				}				
			}			
			return crash;			
		}
		
		private boolean obstacleE(){//동쪽
			boolean crash = false;			
			for(int i = 0; i < 44; i++){
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 1){
					if( Math.abs(CirBall.center.y - rec[i].lowerLeft.y) < Math.abs(CirBall.center.x - rec[i].lowerLeft.x))
						PBEngine.ballx = rec[i].lowerLeft.x - CirBall.radius - CirBall.radius;
					else
						crash = true;
				}
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 4){
					PBEngine.bally = rec[i].lowerLeft.y - CirBall.radius - CirBall.radius;
					crash = true;
				}			
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 7){
					if( Math.abs(CirBall.center.y - rec[i].lowerLeft.y) < Math.abs(CirBall.center.x - (rec[i].lowerLeft.x + rec[i].width)))
						PBEngine.ballx = rec[i].lowerLeft.x + rec[i].width;
					else
						crash = true;
				}	
			}			
			return crash;			
		}
		
		private boolean obstacleW(){//서쪽
			boolean crash = false;			
			for(int i = 0; i < 44; i++){			
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 3){ 
					if( Math.abs(CirBall.center.y - (rec[i].lowerLeft.y + rec[i].height)) < Math.abs(CirBall.center.x - rec[i].lowerLeft.x))
						PBEngine.ballx = rec[i].lowerLeft.x - CirBall.radius - CirBall.radius;
					else
						crash = true;
				}
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 6){ 
					PBEngine.bally =  rec[i].lowerLeft.y + rec[i].height; 
					crash = true;
				}
				if(PBOverlapTester.overlapCircleRectangle(CirBall, rec[i]) == 9){ 
					if( Math.abs(CirBall.center.y - (rec[i].lowerLeft.y + rec[i].height)) < Math.abs(CirBall.center.x - (rec[i].lowerLeft.x + rec[i].width)))
						PBEngine.ballx = rec[i].lowerLeft.x + rec[i].width;
					else
						crash = true;
				}				
			}			
			return crash;			
		}
		
		
		//가속도 이용해서 공의 속도 계산 및 적용
		private void acc(){
			//s= v0t + 1/2at², v = v0 + at 
			//기울임에 따라 공 이동
			
			// y 좌표 조정
			if(-9.8<=PBGame.accX && PBGame.accX<-0.1 && oriY != 2){
				if(PBEngine.bally > 10.06f){ // 오른쪽 테두리
					PBEngine.bally = 10.06f;
					sy = 0;
					vy = 0;
				}
				else{					
					if(obstacleE()){
						sy = 0;
						vy = 0;
					}else{
						sy = vy + (0.5f)*PBGame.accX*(0.016f)*cs;
						vy = vy + PBGame.accX*(0.016f)*cs;
						PBEngine.bally = PBEngine.bally - sy; 
					}					
				}
				oriY = 1;				
			}else	if(-9.8<=PBGame.accX && PBGame.accX<-0.1 && oriY ==2 ){
				sy = 0;
				vy = 0;
				oriY = 1;
			}else if(0.1<PBGame.accX && PBGame.accX<=9.8 && oriY != 1){				
				if(PBEngine.bally < 0.1f){ //왼쪽 테두리
					PBEngine.bally = 0.1f;
					sy = 0;
					vy = 0;
				}
				else{
					if(obstacleW()){
						sy = 0;
						vy = 0;
					}else{
						sy = vy + (0.5f)*PBGame.accX*(0.016f)*cs;
						vy = vy + PBGame.accX*(0.016f)*cs;
						PBEngine.bally = PBEngine.bally - sy;						
					}
				}
				oriY = 2;	
			}else if(0.1<PBGame.accX && PBGame.accX<=9.8 && oriY == 1){
				sy = 0;
				vy = 0;
				oriY = 2;
			}else{
				sy = 0;
				vy = 0;
				oriY = 0;
			}
			
			
			// x 좌표 조정	
			if(-9.8<=PBGame.accY && PBGame.accY<-0.1 && oriX != 2 ){
				if(PBEngine.ballx < 0.09f){ //상단 테두리
					PBEngine.ballx = 0.09f;
					sx = 0;
					vx = 0;
				}
				else{
					if(obstacleN()){ //장애물에 부딪혔을 때 obstacle 함수를 불러 처리함.
						sx = 0;
						vx = 0;
					}else{
						sx = vx + (0.5f)*PBGame.accY*(0.016f)*cs;
						vx = vx + PBGame.accY*(0.016f)*cs;
						PBEngine.ballx = PBEngine.ballx + sx;						
					}
				}
				oriX = 1;
			}else if(-9.8<=PBGame.accY && PBGame.accY<-0.1 && oriX == 2 ){
				sx = 0;
				vx = 0;
				oriX = 1;
			}else if(0.1<PBGame.accY && PBGame.accY<=9.8 && oriX != 1){
				if(PBEngine.ballx > 18.63f){ //하단 테두리
					PBEngine.ballx = 18.63f;
					sx = 0;
					vx = 0;
				}
				else{
					if(obstacleS()){
						sx = 0;
						vx = 0;
					}else{
						sx = vx + (0.5f)*PBGame.accY*(0.016f)*cs;
						vx = vx + PBGame.accY*(0.016f)*cs;
						PBEngine.ballx = PBEngine.ballx + sx;						
					}
				}
				oriX = 2;
			}else if(0.1<PBGame.accY && PBGame.accY<=9.8 && oriX == 1){
				sx = 0;
				vx = 0;
				oriX = 2;
			}			
			else{
				sx = 0;
				vx = 0;
				oriX = 0;
			}
		}
		
		private void instant(){
			int ran = 1 + (int)(Math.random() * 5);
			switch(ran){
				case 1 :
					PBEngine.ballx = 0.3f;
					PBEngine.bally = 0.3f;
					break;
				case 2 :
					PBEngine.ballx = 1.4f;
					PBEngine.bally = 6.2f;
					break;
				case 3 :
					PBEngine.ballx = 9.7f;
					PBEngine.bally = 8.7f;
					break;
				case 4 :
					PBEngine.ballx = 5.6f;
					PBEngine.bally = 5.0f;
					break;
				case 5 :
					PBEngine.ballx = 14.0f;
					PBEngine.bally = 3.7f;
					break;
			}
		}
		
		private void ball(GL10 gl){
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glLoadIdentity();
			gl.glPushMatrix();
						
			acc();//가속도 적용
			for(int i = 0; i < 3; i++){
				if(inhole[i]){
					instant();
					inhole[i] = false;
				}
			}
			CirBall.trans(PBEngine.ballx+0.475f, PBEngine.bally+0.475f);//공의 경계도형을 공의 움직임에 맞춰 이동
		   			
			gl.glScalef(0.078f * 0.65f, 0.138f * 0.65f, 0.1f * 0.65f);
			gl.glTranslatef(PBEngine.ballx, PBEngine.bally, 2f);
			gl.glMatrixMode(GL10.GL_TEXTURE);
			gl.glLoadIdentity();			
			ball.draw(gl);			
			gl.glPopMatrix();
			gl.glLoadIdentity();
		}
		
		private void background(GL10 gl){
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glLoadIdentity();			
			gl.glPushMatrix();
			gl.glScalef(1f, 1f, 1f);
			gl.glTranslatef(0f, 0f, 0f);			
			gl.glMatrixMode(GL10.GL_TEXTURE);
			gl.glLoadIdentity();			
			background.draw(gl);
			gl.glPopMatrix();
			gl.glLoadIdentity();			
		}
					
		private void coin(GL10 gl){			
			for(int i=0; i < 9; i++){
				if(PBEngine.drawcoin[i] == 1){
					gl.glMatrixMode(GL10.GL_MODELVIEW);
					gl.glLoadIdentity();
					gl.glPushMatrix();
					gl.glScalef(0.078f * 0.65f, 0.138f * 0.65f, 0.1f * 0.65f);
					gl.glTranslatef(vec[i].x, vec[i].y, 2f);						
					gl.glMatrixMode(GL10.GL_TEXTURE);
					gl.glLoadIdentity();						
					coin[i].draw(gl);			
					gl.glPopMatrix();
					gl.glLoadIdentity();
				}
			}
		}
			
		private void clock(GL10 gl){
				gl.glMatrixMode(GL10.GL_MODELVIEW);
				gl.glLoadIdentity();
				gl.glPushMatrix();
				gl.glScalef(0.078f * 0.65f, 0.138f * 0.65f, 0.1f * 0.65f);
				gl.glTranslatef(clockVec.x, clockVec.y, 2f);						
				gl.glMatrixMode(GL10.GL_TEXTURE);
				gl.glLoadIdentity();						
				clock.draw(gl);			
				gl.glPopMatrix();
				gl.glLoadIdentity();
		}
		
		private void speed(GL10 gl){			
				gl.glMatrixMode(GL10.GL_MODELVIEW);
				gl.glLoadIdentity();
				gl.glPushMatrix();
				gl.glScalef(0.078f * 0.65f, 0.138f * 0.65f, 0.1f * 0.65f);
				gl.glTranslatef(speedVec.x, speedVec.y, 2f);						
				gl.glMatrixMode(GL10.GL_TEXTURE);
				gl.glLoadIdentity();						
				speed.draw(gl);			
				gl.glPopMatrix();
				gl.glLoadIdentity();
		}
		
		@Override
		public void onSurfaceChanged(GL10 gl, int width, int height) {
			gl.glViewport(0, 0, width, height);
			float wf = (float) width / (float) 1280;
			float hf = (float) height / (float) 720;			
			gl.glMatrixMode(GL10.GL_PROJECTION);
			gl.glLoadIdentity();
			gl.glOrthof(0f, 1f * wf, 0f, 1f * hf, -1f, 1f); //직교 2D 형태
		}

		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig config) {
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glClearDepthf(1.0f);
			gl.glEnable(GL10.GL_DEPTH_TEST);
			gl.glDepthFunc(GL10.GL_LEQUAL);		
			
		    background.loadTexture(gl, PBEngine.BACKGROUND_LAYER_THREE, PBEngine.context);		
		    ball.loadTexture(gl, PBEngine.PLAYER_BALL, PBEngine.context);
		    for(int i=0; i<9; i++){
		    	coin[i].loadTexture(gl, PBEngine.PLAYER_COIN, PBEngine.context);  
		    }
			clock.loadTexture(gl, PBEngine.PLAYER_CLOCK, PBEngine.context);
			speed.loadTexture(gl, PBEngine.PLAYER_SPEED, PBEngine.context);

		}
		
}